package com.disruptor.sample._02producer.run;

import com.disruptor.sample._01base.consumer.LongEventHandler;
import com.disruptor.sample._01base.factory.LongEventFactory;
import com.disruptor.sample._02producer.procucer.LongEventProducer;
import com.disruptor.sample.common.dto.LongEvent;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;

public class LongEventMainByProducer {

  public static void main(String[] args) throws Exception {
    LongEventFactory factory = new LongEventFactory();

    int bufferSize = 1024;
    Disruptor<LongEvent> disruptor =
        new Disruptor<>(factory, bufferSize, DaemonThreadFactory.INSTANCE);

    disruptor.handleEventsWith(new LongEventHandler());
    disruptor.start();

    RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
    LongEventProducer producer = new LongEventProducer(ringBuffer);
    ByteBuffer bb = ByteBuffer.allocate(8);
    for (long l = 0; true; l++) {
      bb.putLong(0, l);
      producer.onData(bb);
      Thread.sleep(1000);
    }
  }

}
